<?php
namespace app\common\controller;

/**
 * 工作记录每日数据类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/21
 * Time: 15:46
 */
class WorkRecordData{

    /**
     * 获取每日数据目前金额
     * @param $department
     * @param $now_date
     * @return float|int
     */
    public function get_month_now_money($department,$now_date){
        $month_time = get_the_month_time($now_date);
        $start_date = date('Y-m-d',strtotime($month_time['start_time']));
        $end_date = date('Y-m-d',strtotime($month_time['end_time']));

        $maps = [];
        $maps[] = ['department','=',$department];
        $maps[] = ['now_date','between',[$start_date,$end_date]];
        $maps[] = ['now_date','<',$now_date];
        $now_money = model('work_record_data')->get_sum($maps,'today_total_money');
        return deal_number_div_100($now_money);
    }


    /**
     * 获取每日数据目标金额
     * @param $department
     * @param $start_time
     * @return float|int
     */
    public function get_month_target_money($department,$start_time){
        $year = date('Y',strtotime($start_time));
        $month = date('n',strtotime($start_time));

        $maps = [];
        $maps[] = ['year','=',$year];
        $maps[] = ['department','=',$department];
        $wdtm_detail = model('wr_data_target_money')->get_detail($maps,'id,month_target_money');
        $month_target_money = !empty($wdtm_detail) ? json_decode($wdtm_detail['month_target_money'],true) : [];
        if(!empty($wdtm_detail) && !empty($month_target_money) && isset($month_target_money[$month]) && $month_target_money[$month] > 0){
            $target_money = $month_target_money[$month];
        }else{
            $target_money = 100;
        }
        return deal_number_div_100($target_money);
    }

    /**
     * 获取每日数据统计
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function get_total($department,$start_time,$end_time){
        $wrd_maps = [];
        $wrd_maps[] = ['department','=',$department];
        if(!empty($start_time) && !empty($end_time)){
            $start_date = date('Y-m-d',strtotime($start_time));
            $end_date = date('Y-m-d',strtotime($end_time));
            $wrd_maps[] = ['now_date','between',[$start_date,$end_date]];
        }
        $list = model('work_record_data')->get_list($wrd_maps);

        $now_money = 0;
        $target_money = 0;

        $start_day = date('d',strtotime($start_time));
        $start_month = date('Y-m',strtotime($start_time));
        $end_month = date('Y-m',strtotime($end_time));
        if($start_day == '01' && $start_month == $end_month){
            $today_total_money = model('work_record_data')->get_sum($wrd_maps,'today_total_money');
            //if(!empty($today_total_money)){
                $now_money = $today_total_money;
                $target_money = $this->get_month_target_money($department,$start_time);
            //}
        }

        $today_shop_person_num = 0;
        $front_money = 0;
        $vip_back_money = 0;
        $check_in_back_money = 0;
        $agency_back_money = 0;
        $account_back_money = 0;
        $trademark_back_money = 0;
        $science_money = 0;
        $other_money = 0;
        foreach($list as $k => $v){
            $today_shop_person_num += $v['today_shop_person_num'];
            $front_money += $v['front_money'];
            $vip_back_money += $v['vip_back_money'];
            $check_in_back_money += $v['check_in_back_money'];
            $agency_back_money += $v['agency_back_money'];
            $account_back_money += $v['account_back_money'];
            $trademark_back_money += $v['trademark_back_money'];
            $science_money += $v['science_money'];
            $other_money += $v['other_money'];
        }

        $data['today_shop_person_num'] = $today_shop_person_num;
        $data['front_money'] = deal_number_div_100($front_money);
        $data['vip_back_money'] = deal_number_div_100($vip_back_money);
        $data['check_in_back_money'] = deal_number_div_100($check_in_back_money);
        $data['agency_back_money'] = deal_number_div_100($agency_back_money);
        $data['account_back_money'] = deal_number_div_100($account_back_money);
        $data['trademark_back_money'] = deal_number_div_100($trademark_back_money);
        $data['science_money'] = deal_number_div_100($science_money);
        $data['other_money'] = deal_number_div_100($other_money);
        $data['now_money'] = deal_number_div_100($now_money);
        $data['target_money'] = $target_money;
        return $data;
    }
}